﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Catalog;

namespace OnlineFloralShop
{
    public partial class AddToCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          //Get link AddToCart Referrer
            if (!Page.IsPostBack)
            {
                if (Page.Request.UrlReferrer != null)
                {
                    Session["AddToCartReferrer"] = Page.Request.UrlReferrer.ToString();
                }
            }
            
            string ProId = "";
            int Quantity;
            //If not have QueryString["ProductId"] return Default page
            if (Request.QueryString["ProductId"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                //If Add Item on List Product Page
                if (Request.QueryString["Quantity"] == null)
                { Quantity = 1; }
                else
                {
                    //Add Item on Details page
                    Quantity = int.Parse(Request.QueryString["Quantity"].ToString());
                }

                //
                ProId = Request.QueryString["ProductId"];

                //Check (so luong add + so luong da add) co nhiu hon trong data khong
                int itemadded;
                if (((Dictionary<string, int>)Session["cart"]).ContainsKey(ProId))
                {
                    itemadded = int.Parse(((Dictionary<string, int>)Session["cart"])[ProId].ToString());
                }
                else
                {
                    itemadded = 0;
                }

                if (Common.CommonController.CheckQuantityAddToCart(ProId, Quantity+itemadded))
                {
                    if (Session["AddToCartReferrer"] != null)
                    {
                    //Thong bao va lua chon quay ve trang truoc
                    Label_checkQuantity.Text = "Item(s) update more than item(s) in Stock! <a href='"+Session["AddToCartReferrer"].ToString()+"'>Go back</a>"
                        + " | <a href='Cart.aspx'>View cart</a>";

                    }
                }
                else
                {

                    //Check Product is exist
                    if (((Dictionary<string, int>)Session["cart"]).ContainsKey(ProId))
                    {
                        //Update Quantity
                        ((Dictionary<string, int>)Session["cart"])[ProId] += Quantity;
                    }
                    else
                    {
                        //Add new item to cart
                        ((Dictionary<string, int>)Session["cart"]).Add(ProId, Quantity);
                    }
                    //Go to Cart
                    Response.Redirect("Cart.aspx");
                }
           
            }
        }

       
       
    }
}
